package com.cyzy.controller;

import com.cyzy.dto.Result;
import com.cyzy.pojo.Coupon;
import com.cyzy.service.CouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.websocket.server.PathParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @GetMapping("/selectCouponList")
    public Result selectCouponList(int page,int size){
        // 启动分页，page表示当前页，size表示每页的记录数
        PageHelper.startPage(page, size);

        // 获取分页后的数据
        List<Coupon> selectCouponList = couponService.selectCouponList();
        if(selectCouponList == null){
            return Result.ok().message("查询失败！");
        }
        PageInfo<Coupon> pageInfo = new PageInfo<>(selectCouponList);

        return Result.ok().message("查询成功").data("selectCouponList",selectCouponList).data("count",pageInfo.getTotal());

    }

    @PostMapping("/addCoupon")
    public Result addCoupon(@RequestBody Coupon coupon){

        System.out.println(coupon+"-------------coupon---------------");

        coupon.setRemainingCount(coupon.getTotalCount());
        int save = couponService.addCoupon(coupon);
        if(save == 0){
            return Result.ok().message("添加失败！");
        }
        return Result.ok().message("添加成功！");
    }

    @PutMapping("/updateCoupon")
    public Result updateCoupon(@RequestBody Coupon coupon){

        boolean b = couponService.updateById(coupon);
        if (!b){
            return Result.ok().message("修改失败！");
        }
        return Result.ok().message("修改成功！");
    }

    @DeleteMapping("/deleteCoupon")
    public Result deleteCoupon(@RequestParam int templateId){

        int i = couponService.deleteCoupon(templateId);
        if (i == 0){
            return Result.ok().message("删除失败！");
        }
        return Result.ok().message("删除成功！");
    }

    @DeleteMapping("/deleteCoupons")
    public Result deleteCoupons(@RequestParam List<Long> templateIds){

        System.out.println(templateIds+"--------templateIds--------");
        int b = couponService.deleteCoupons(templateIds);
        if (b == 0){
            return Result.ok().message("删除失败！");
        }
        return Result.ok().message("删除成功！");
    }



}
